<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增型号</title>
    <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
    <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
    <script type="text/javascript" src="/webapp/js/common/config.js"></script>
    <script type="text/javascript" src="/webapp/js/common/metadata.js"></script>
</head>
<body>
<div class="easyui-container-fluid">
    <div class="easyui-panel" style="border: none;">
        <div id="nav" class="nav"></div>
        <div class="easyui-panel" style="border:none;">
            <form id="ff" name="model">

                <div style="margin-bottom:20px;">
                    <label class="item-label"><span class="item-required">*</span>品类：</label>
                    <select class="easyui-combobox" id="categorySelect" name="categoryCode" style="width:128px;" required="required" >
                    </select>
                    <label class="item-label"><span class="item-required">*</span>品牌：</label>
                    <select class="easyui-combobox" id="brandSelect" name="brandCode" style="width:128px;" validType="notNull['#brandSelect']" required="required" >
                    </select>
                    <a class='l-btn-left l-btn-icon-left' style='width:22px;height: 24px' onclick="aa()"><span class='l-btn-icon icon-reload'></span></a>
                    <label id="nameDiv">
                        <label class="item-label"><span class="item-required">*</span>型号：</label>
                        <input type="text" class="easyui-textbox " name="name" id="modelName"  data-options="required:true"/>
                    </label>

                    <label class="item-label">系列：</label>
                    <input type="text" class="easyui-textbox" name="series" id="series" style="width: 134px"/>
                    <select class="easyui-combobox" id="seriesTo3cCode" name="seriesTo3cCode" style="width:128px;display: none" ></select>
                    <a class="easyui-linkbutton c8" href="javascript:void(0);" onclick="searchBymodelf()" style="width:80px;margin-left: 3px;">型号确认</a>&nbsp;&nbsp;
                </div>
                <div style="margin-bottom:20px;">
                    <label class="item-label">别名：</label>
                    <input name="otherNames" class="easyui-tagbox" value="" style="width:444px;min-height:60px;" data-options="multiline:true"/>
                    <label class="item-label" style="margin:10px 0;"></span>产品卖点：</label>
                    <input type="text" class="easyui-textbox" style="width:400px;height:60px;" data-options="multiline:true"name="sellMerit"/>
                </div>
                <div style="margin-bottom:20px;">
                    <label class="item-label" >上市年份：</label>
                    <input type="text" class="easyui-numberbox" name="onSellYear" id="onSellYear" />
                    <label class="item-label" ><span class="item-required">*</span>上市价格：</label>
                    <input type="text" class="easyui-numberbox" data-options="required:true" prompt="请输入上市价格" name="onSellPrice"  />
                    <label class="item-label" ><span class="item-required">*</span>规格状态：</label>
                    <select name="propStatus" class="easyui-combobox"  data-options="required:true,panelHeight:'auto'"  style="width:100px;" >
                        <option value="CONFIRMONE" selected>部分确认</option>
                        <!--<option value="CONFIRMTWO">两级</option>-->
                        <!--<option value="CONFIRMTHREE">三级</option>-->
                        <option value="CONFIRMALL">全部确认</option>
                        <option value="UNCOFIRMED">未确认</option>
                        <option value="UNABLECOFIRED">无法找到信息</option>
                        <option value="UNRELEASED">暂未发布</option>
                    </select>
                </div>
                <div style="margin-bottom:20px;">
                    <label class="item-label" >上市月：</label>
                    <input type="text" class="easyui-numberbox" name="onSellMonth" id="onSellMonth" />
                    <label class="item-label" >上市周：</label>
                    <input type="text" class="easyui-numberbox" name="onSellWeek" id="onSellWeek"/>
                    <!--<label class="item-label" ><span class="item-required">*</span>所属组数：</label>-->
                    <!--<select name="group" class="easyui-combobox"  data-options="required:true,panelHeight:'auto'"  style="width:100px;" >-->
                        <!--<option value="1" selected>第一组</option>-->
                        <!--<option value="2">第二组</option>-->
                        <!--<option value="3">第三组</option>-->
                    <!--</select>-->
                </div>
                <div style="margin-bottom:20px;">
                    <label class="item-label">描述：</label>
                    <input type="text" class="easyui-tagbox" name="description"  id="description" data-options="multiline:true" style="width:868px;min-height:60px"/>
                    <a onclick="tzwy()" target="_Blank"  style="width:80px;margin-left: 10px;">跳转</a>&nbsp;&nbsp;<!--onclick="tzwy()"-->
                    <a class='l-btn-left l-btn-icon-left' style='width:50px;height: 50px' onclick='copyUrl()'><span class='l-btn-icon icon-copy'></span></a>

                </div>
                <div >
                    &nbsp;&nbsp;<span style="font-weight: 900 ;font-size:20px; width: 100px" >规格数据：</span>

                </div>
                <div id="categorymetadatawrapper"></div>
                <div style="margin-bottom:20px;margin-left:100px;">
                    <a href="javascript:void(0)" class="easyui-linkbutton c8" onclick="submitForm()" style="width:80px">保存</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="cancel()" style="width:80px">取消</a>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
    var id=getQueryParameter("id"),categoryCode=getQueryParameter("categoryCode");
    var rwId=getQueryParameter("rwId")
    var categoryType;
    var seriesTo3cCode;
    $(function(){
        setNav();
        getCategoryType();
        if(null != id){
            $.when(initBrandSelect("brandSelect", function () {initSeries(categoryCode,"brandSelect")}),
                initCategorySelect("categorySelect"),
                generatemetadatawrap(false,true)
            ).done(function(){initFormData()});
        }else{
            $.when(initBrandSelect("brandSelect", function () {initSeries(categoryCode,"brandSelect")}),
                initCategorySelect("categorySelect"),
                generatemetadatawrap(true,true)
            ).done(function(){initFormData()});
        }
        initOnSellYearMonth();
    });

    function getCategoryType() {
        $.ajax({
            url: config.url + "/category/get",
            type: "get",
            data: {code:categoryCode},
            xhrFields: {withCredentials: true},
            success: function (data) {
                if (data.code == 200) {
                    categoryType = data.msg.type;
                    if (categoryType == 2) {
                        $("#seriesTo3cCode").next().show();
                        $("#series").next().hide();
                        $("#nameDiv").hide();
                        $("#modelName").textbox("setValue", "1"); //3c产品不用写型号，初始化name用于表单验证
                    }else {
                        $("#seriesTo3cCode").next().hide();
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            }
        });
    }

    function tzwy() {
        var  wz=$("#description").tagbox('getValues');
        if(wz != null && wz !=""){
            wz.forEach(function(v){
                window.open(v);
                // document.getElementById("tzwy").href = v;
            });

        }
    }
    function generatemetadatawrap(au,tsTj) {//au为布尔类型，当为true时表示添加数据
        $.ajax({
            url:config.url+"/categoryMetadata/getMetadataUnderCategory?categoryCode="+categoryCode+"&sfTj="+tsTj,
            type:'get',
            xhrFields:{withCredentials:true},
            success:function (data) {
                //规格数据填充
                var content = generateContent(data,au);
                $("#categorymetadatawrapper").html(content);
                $.parser.parse($("#categorymetadatawrapper"));

                /**
                 * 针对智能门锁进行关联项
                 */

                $(".zw").change(function(a) {
                    if($(".zw").prop('checked')){
                        $("#04774").combobox("setValue","是");
                        // alert("选中");
                        //当前为选中状态
                    }else {
                        $("#04774").combobox("setValue","否");
                        // alert("未选中");
                        //当前为不选中状态
                    }
                });
                $(".mm").change(function(a) {
                    if($(".mm").prop('checked')){
                        $("#04775").combobox("setValue","是");
                        //当前为选中状态
                    }else {
                        $("#04775").combobox("setValue","否");
                        //当前为不选中状态
                    }
                });

                $(".ys").change(function(a) {
                    if($(".ys").prop('checked')){
                        $("#04776").combobox("setValue","是");
                        //当前为选中状态
                    }else {
                        $("#04776").combobox("setValue","否");
                        //当前为不选中状态
                    }
                });
                $(".sk").change(function(a) {
                    if($(".sk").prop('checked')){
                        $("#04777").combobox("setValue","是");
                        //当前为选中状态
                    }else {
                        $("#04777").combobox("setValue","否");
                        //当前为不选中状态
                    }
                });
                $(".app").change(function(a) {
                    if($(".app").prop('checked')){
                        $("#04778").combobox("setValue","是");
                        //当前为选中状态
                    }else {
                        $("#04778").combobox("setValue","否");
                        //当前为不选中状态
                    }
                });
                $(".rlsb").change(function(a) {
                    if($(".rlsb").prop('checked')){
                        $("#04779").combobox("setValue","是");
                        //当前为选中状态
                    }else {
                        $("#04779").combobox("setValue","否");
                        //当前为不选中状态
                    }
                });
                $(".wx").change(function(a) {
                    if($(".wx").prop('checked')){
                        $("#04780").combobox("setValue","是");
                        //当前为选中状态
                    }else {
                        $("#04780").combobox("setValue","否");
                        //当前为不选中状态
                    }
                });
                $(".hm").change(function(a) {
                    if($(".hm").prop('checked')){
                        $("#04781").combobox("setValue","是");
                        //当前为选中状态
                    }else {
                        $("#04781").combobox("setValue","否");
                        //当前为不选中状态
                    }
                });
                $(".zjm").change(function(a) {
                    if($(".zjm").prop('checked')){
                        $("#04782").combobox("setValue","是");
                        //当前为选中状态
                    }else {
                        $("#04782").combobox("setValue","否");
                        //当前为不选中状态
                    }
                });
                $(".ly").change(function(a) {
                    if($(".ly").prop('checked')){
                        $("#04783").combobox("setValue","是");
                        //当前为选中状态
                    }else {
                        $("#04783").combobox("setValue","否");
                        //当前为不选中状态
                    }
                });

            }
        });
    }
    function initFormData(){
        if(id){
            $.ajax({
                url:config.url +"/model/"+id,
                type:'get',
                xhrFields:{withCredentials:true},
                success:function(data){
                    //填充表单
                    if(data){
                        var formdata = changeProperty2String(data);
                        // console.log(formdata);
                        if(!formdata.otherNames){
                            formdata.otherNames=[];
                        }
                        if(!formdata.description){
                            formdata.description=[];
                        }
                        if(formdata.properties){
                            var props = formdata.properties;
                            for(var i in props){
                                formdata[props[i].code] = props[i].value;
                                if("boolean"==(typeof props[i].value)){
                                    formdata[props[i].code] = props[i].value.toString();
                                }
                            }
                        }
                        seriesTo3cCode = formdata.seriesTo3cCode;
                        $("#ff").form("load",formdata);
                        //model brand readonly
                        $("#brandSelect").combobox("readonly",true);
                    }
                },
                error:function(){

                }
            });
        }
        $("#categorySelect").combobox("select",categoryCode);
        $("#categorySelect").combobox("readonly",true);
    }
    function submitForm(){
        $("#ff").form("submit",{
            onSubmit:function(){
                var valid = $(this).form('enableValidation').form('validate');

                if(valid){
                    var formData = getFormData(document.getElementById("ff"));
                    formData = transfer(formData);
                    var method = "POST";
                    if(id){
                        method = "PATCH";
                        formData.id = id;
                    }
                    if(formData['otherNames'] && typeof formData['otherNames'] === 'string'){
                        formData['otherNames'] = [formData['otherNames']]
                    }
                    // if(formData['description'] && typeof formData['description'] === 'string'){
                    //     formData['description'] = [formData['description']]
                    // }
                    var properties = [];
                    for(var i in formData){
                        if(!isNaN(i)&&formData[i]){
                            properties.push({code:i,value:formData[i]});
                        }
                    }
                    formData['properties'] = properties;
                    var description = [];
                    var wz = $("#description").tagbox('getValues');
                    wz.forEach(function(v){
                        description.push(v);
                    });
                    formData['description'] = description;

                    //验证系列
                    var series = formData["series"];
                    var reg = /^(\w|[\u4E00-\u9FA5]|\(|\)|\/|-)*$/;
                    if (series) {
                        if (series.length > 20) {
                            $.messager.alert("提示","【系列】长度不能超过20个字符","error");
                            return;
                        }
                        // else if(!series.match(reg)){
                        //     $.messager.alert("提示","【系列】不能含有\"()/-\"以外的特殊符号","error");
                        //     return;
                        // }
                    }

                    if(formData['onSellPrice']<=0){
                        $.messager.alert("提示","上市价格不能小于等于0","error");
                        return;
                    }

                    var  name=$("#modelName").textbox('getValue');
                    formData.name=name;
                    formData.rwId = rwId;

                    if (!id) {
                        if (!formData['onSellYear']) {
                            formData['onSellYear'] = new Date().getFullYear();
                        }
                        if (!formData['onSellMonth']) {
                            formData['onSellMonth'] = new Date().getMonth()+1;
                        }
                        if (!formData['onSellWeek']) {
                            formData['onSellWeek'] = window.parent.timeData.weekInYear;;
                        }
                    }

                    $.ajax({
                        url:config.url+"/modelConfirm",
                        type:method,
                        data:JSON.stringify(formData),
                        dataType:'json',
                        contentType:'application/json;charset=UTF-8',
                        xhrFields:{withCredentials:true},
                        beforeSend:function(){
                            $.messager.progress({
                                title:"请稍等",
                                interval:50,
                                text:"操作进行中..."
                            });
                        },
                        complete:function(){
                            $.messager.progress("close");
                        },
                        success:function(data){
                            // console.log(data.code);
                            if(data.code=="200"){
                                $.messager.alert("提示",data.msg,"info",function(){
                                    window.parent.closeCurrentTab(true);
                                });
                            }else{
                                $.messager.alert("提示",data.msg,"error");
                            }
                        },
                        error:function(XMLHttpRequest,textStatus,thrown){
                            $.messager.alert("提示","操作失败","error");
                        }
                    });
                    return false;
                }
                return false;
            }
        });
    }
    function aa(){
        $.ajax({
            url:config.url + "/cron/cache/data/refresh",
            type:'get',
            xhrFields:{withCredentials:true},
            beforeSend:function () {
                $.messager.progress({
                    title:"请稍等",
                    interval:50,
                    text:"数据加载中..."
                });
            },
            complete:function () {
                $.messager.progress("close");
            },
            success:function (data) {

            },
            error:function (XMLHttpRequest,textStatus,errorThrown) {

            }
        });
        initBrandSelect("brandSelect");
    }
    function cancel(){
        window.parent.closeCurrentTab();
    }

    function searchBymodelf() {
        var params = {};
        var formdata = getFormData(document.getElementById("ff"));
        params.categoryCode = formdata.categoryCode;
        params.brandCode = formdata.brandCode;
        params.name = $('#modelName').textbox('getValue');
        params.model = true;
        $.ajax({
            url: config.url + "/workshop/modelCheck",
            type: "post",
            data: params,
            xhrFields: {withCredentials: true},
            success: function (data) {
                if (data == "") {
                    $.messager.alert("提示", "该型号不存在，可以添加", "info")
                } else {
                    $.messager.alert("提示", data, "error");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            }
        });
    }

    function copyUrl(){
        var  url=$("#description").textbox('getValue');
        // console.log(url);
        var flag = copyText(url);
        $.messager.show({
            title: '提示',
            msg: flag?"网址复制成功":"网址复制失败",
            timeout: 500,
            showType: 'show'
        });
    }

    function initOnSellYearMonth() {
        if(!id){
            var myDate = new Date();
            var year = myDate.getFullYear();//获取年
            var month = myDate.getMonth() + 1;//获取月，默认从0开始，所以要加一
            $('#onSellYear').numberbox({prompt:year});
            $('#onSellMonth').numberbox({prompt:month});
            var week = window.parent.timeData.weekInYear;
            $('#onSellWeek').numberbox({prompt:week});
        }
    }





    // 反转义

    function HTMLDecode(text) {
        var temp = document.createElement("div");
        temp.innerHTML = text;
        var output = temp.innerText || temp.textContent;
        temp = null;
        return output;
    }

    //HTML转义
    function HTMLEncode(html) {
        var temp = document.createElement("div");
        (temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html);
        var output = temp.innerHTML;
        temp = null;
        return output;
    }




</script>
</html>